package com.wmlive.hhvideo.heihei.personal.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.wmlive.hhvideo.R;

import com.wmlive.hhvideo.common.base.DcBaseActivity;
import com.wmlive.hhvideo.heihei.personal.location.AmapLocationManagerImpl;
import com.wmlive.hhvideo.heihei.personal.location.LocationApi;
import com.wmlive.hhvideo.heihei.personal.location.PerfectLocation;
import com.wmlive.hhvideo.utils.ToastUtil;

import butterknife.BindView;

/**
 * Created by XueFei on 2017/6/3.
 * <p>
 * 城市定位
 */

public class LocationActivity extends DcBaseActivity {
    private LocationApi mLocationApi;
    private String cityName = "";

    // TODO: 2017/6/2 城市获取
    private LocationApi.LocationCallback mLocationCallback = new LocationApi.LocationCallback() {

        @Override
        public void locationSuccess(PerfectLocation location) {
            try {
                imgLocation.setImageResource(R.drawable.icon_profile_place_nor);
                cityName = location.cityName;
                textCity.setText(cityName);
                textCity.setTextColor(getResources().getColor(R.color.hh_color_a));
//                latlng = location.latitude + "," + location.longitude;
                mLocationApi.stopLocation();
            } catch (IllegalStateException e) {
                mLocationApi.stopLocation();
            }
        }

        @Override
        public void locationFailed() {
            try {
                imgLocation.setImageResource(R.drawable.icon_profile_place_dis);
                textCity.setText(getText(R.string.user_location_fail));
                mLocationApi.stopLocation();
            } catch (IllegalStateException e) {
                mLocationApi.stopLocation();
            }
        }
    };

    @BindView(R.id.btn_back)
    RelativeLayout btnBlack;
    @BindView(R.id.img_location)
    ImageView imgLocation;
    @BindView(R.id.text_city)
    TextView textCity;
    @BindView(R.id.tv_location_permission)
    TextView tvLocationPermission;
    @BindView(R.id.tv_location_permission_set)
    TextView tvLocationPermissionSet;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_location;
    }

    @Override
    protected void initData() {
        super.initData();

        setTitle(R.string.user_location_ttitle, true);

        btnBlack.setOnClickListener(this);

        mLocationApi = createLocationApi(LocationApi.LocationMode.AMAPLOCATION);

        PackageManager pkgManager = getPackageManager();
        boolean locationPermission =
                pkgManager.checkPermission(Manifest.permission.ACCESS_FINE_LOCATION, getPackageName()) == PackageManager.PERMISSION_GRANTED;
        if (Build.VERSION.SDK_INT >= 23 && !locationPermission) {
            tvLocationPermission.setText(getString(R.string.user_location_permission_faid));
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_PHONE_STATE},
                    0);
        } else {
            mLocationApi.startLocation(mLocationCallback);
            tvLocationPermission.setText(getString(R.string.user_location_permission_suc));
        }
    }

    @Override
    protected void onSingleClick(View v) {
        if (v.getId() == R.id.btn_back) {
            if (!TextUtils.isEmpty(cityName)) {
                Intent intent = new Intent();
                intent.putExtra("city", cityName);
                setResult(Activity.RESULT_OK, intent);
                finish();
            } else {
                finish();
            }
        }
    }

    public LocationApi createLocationApi(LocationApi.LocationMode locationMode) {
        LocationApi locationApi = null;
        switch (locationMode) {
            case AMAPLOCATION:
                locationApi = AmapLocationManagerImpl.getInstance(this);
                break;
            default:
                break;
        }
        return locationApi;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == 0) {
            if (grantResults.length > 1) {
                mLocationApi.startLocation(mLocationCallback);
                tvLocationPermission.setText(getString(R.string.user_location_permission_suc));
            } else {
                mLocationApi.stopLocation();
                tvLocationPermission.setText(getString(R.string.user_location_permission_faid));
                ToastUtil.showToast("定位权限禁止后定位功能不能正常使用，请在应用程序管理中开启权限");
            }
        } else {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mLocationApi.stopLocation();
    }
}
